package com.lxr.test5002;

import static android.view.KeyEvent.KEYCODE_0;
import static android.view.KeyEvent.KEYCODE_1;
import static android.view.KeyEvent.KEYCODE_2;
import static android.view.KeyEvent.KEYCODE_3;
import static android.view.KeyEvent.KEYCODE_4;
import static android.view.KeyEvent.KEYCODE_5;
import static android.view.KeyEvent.KEYCODE_6;
import static android.view.KeyEvent.KEYCODE_7;
import static android.view.KeyEvent.KEYCODE_8;
import static android.view.KeyEvent.KEYCODE_9;
import static android.view.KeyEvent.KEYCODE_BACK;
import static android.view.KeyEvent.KEYCODE_ENTER;
import static android.view.KeyEvent.KEYCODE_MINUS;
import static android.view.KeyEvent.KEYCODE_PLUS;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 类描述:[按键测试]
 * 说明:
 *
 * @author: LXR-zsm
 * @Date: 2023/12/15 16:54
 */
public class KeyActivity extends Activity {
    private static final int KEYCODE_DPAD_F1 = 131;
    private static final int KEYCODE_DPAD_F2 = 132;
    private static final int KEYCODE_DPAD_SCAN1 = 139;
    private static final int KEYCODE_DPAD_SCAN2 = 138;
    private static final int KEYCODE_EDIT = 134;
    private static final int KEYCODE_MORE = 133;
    private static final int KEYCODE_LEFT = 140;
    private static final int KEYCODE_RIGHT = 141;
    private boolean keyPressR;
    private boolean keyPressL;
    private boolean keyPressScan1;
    private boolean keyPressScan2;
    private boolean keyPressMore;
    private boolean keyPressEnter;
    private boolean keyPressJian;
    private boolean keyPressEdit;
    private boolean keyPressJia;
    private boolean keyPressF1;
    private boolean keyPressF2;
    private boolean keyPressK0;
    private boolean keyPressK1;
    private boolean keyPressK2;
    private boolean keyPressK3;
    private boolean keyPressK4;
    private boolean keyPressK5;
    private boolean keyPressK6;
    private boolean keyPressK7;
    private boolean keyPressK8;
    private boolean keyPressK9;
    private boolean keyPressBack;

    private TextView mKeyRight;
    private TextView mKeyLeft;
    private TextView mKeyScan1;
    private TextView mKeyScan2;
    private TextView mKeyMore;
    private TextView mKeyEnter;
    private TextView mKeyJian;
    private TextView mKeyEdit;
    private TextView mKeyJia;
    private TextView mKeyF1;
    private TextView mKeyF2;
    private TextView mKey0;
    private TextView mKey1;
    private TextView mKey2;
    private TextView mKey3;
    private TextView mKey4;
    private TextView mKey5;
    private TextView mKey6;
    private TextView mKey7;
    private TextView mKey8;
    private TextView mKey9, tvKey;
    private TextView mKeyBack;

    private boolean isBack = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_key_board);
        init();
    }

    private void init() {
        mKeyLeft = (TextView) findViewById(R.id.tv_key_left);
        mKeyF1 = (TextView) findViewById(R.id.tv_key_F1);
        mKeyF2 = (TextView) findViewById(R.id.tv_key_F2);
        mKeyScan1 = (TextView) findViewById(R.id.tv_key_scan1);
        mKeyScan2 = (TextView) findViewById(R.id.tv_key_scan2);
        mKeyLeft = (TextView) findViewById(R.id.tv_key_left);
        mKeyRight = (TextView) findViewById(R.id.tv_key_right);
        mKeyBack = (TextView) findViewById(R.id.tv_key_back);
        mKeyJia = (TextView) findViewById(R.id.tv_key_jia);
        mKeyJian = (TextView) findViewById(R.id.tv_key_jian);
        mKeyEdit = (TextView) findViewById(R.id.tv_key_edit);
        mKeyEnter = (TextView) findViewById(R.id.tv_key_enter);
        mKeyMore = (TextView) findViewById(R.id.tv_key_more);
        mKey0 = (TextView) findViewById(R.id.tv_key_0);
        mKey1 = (TextView) findViewById(R.id.tv_key_1);
        mKey2 = (TextView) findViewById(R.id.tv_key_2);
        mKey3 = (TextView) findViewById(R.id.tv_key_3);
        mKey4 = (TextView) findViewById(R.id.tv_key_4);
        mKey5 = (TextView) findViewById(R.id.tv_key_5);
        mKey6 = (TextView) findViewById(R.id.tv_key_6);
        mKey7 = (TextView) findViewById(R.id.tv_key_7);
        mKey8 = (TextView) findViewById(R.id.tv_key_8);
        mKey9 = (TextView) findViewById(R.id.tv_key_9);
        tvKey = (TextView) findViewById(R.id.tvKey);
    }

    private void initUI() {
        keyPressR = false;
        keyPressL = false;
        keyPressScan1 = false;
        keyPressScan2 = false;
        keyPressMore = false;
        keyPressEnter = false;
        keyPressJian = false;
        keyPressEdit = false;
        keyPressJia = false;
        keyPressF1 = false;
        keyPressF2 = false;
        keyPressK0 = false;
        keyPressK1 = false;
        keyPressK2 = false;
        keyPressK3 = false;
        keyPressK4 = false;
        keyPressK5 = false;
        keyPressK6 = false;
        keyPressK7 = false;
        keyPressK8 = false;
        keyPressK9 = false;
        keyPressBack = false;

        mKeyRight.setBackgroundResource(R.drawable.textview_border);
        mKeyLeft.setBackgroundResource(R.drawable.textview_border);
        mKeyScan1.setBackgroundResource(R.drawable.textview_border);
        mKeyScan2.setBackgroundResource(R.drawable.textview_border);
        mKeyMore.setBackgroundResource(R.drawable.textview_border);
        mKeyEnter.setBackgroundResource(R.drawable.textview_border);
        mKeyJian.setBackgroundResource(R.drawable.textview_border);
        mKeyEdit.setBackgroundResource(R.drawable.textview_border);
        mKeyJia.setBackgroundResource(R.drawable.textview_border);
        mKeyF1.setBackgroundResource(R.drawable.textview_border);
        mKeyF2.setBackgroundResource(R.drawable.textview_border);
        mKey0.setBackgroundResource(R.drawable.textview_border);
        mKey1.setBackgroundResource(R.drawable.textview_border);
        mKey2.setBackgroundResource(R.drawable.textview_border);
        mKey3.setBackgroundResource(R.drawable.textview_border);
        mKey4.setBackgroundResource(R.drawable.textview_border);
        mKey5.setBackgroundResource(R.drawable.textview_border);
        mKey6.setBackgroundResource(R.drawable.textview_border);
        mKey7.setBackgroundResource(R.drawable.textview_border);
        mKey8.setBackgroundResource(R.drawable.textview_border);
        mKey9.setBackgroundResource(R.drawable.textview_border);
        mKeyBack.setBackgroundResource(R.drawable.textview_border);


    }


    private SystemPropertiesProxy systemPropertiesProxy = new SystemPropertiesProxy();

    @Override
    protected void onResume() {
        super.onResume();
        systemPropertiesProxy.set("persist.open.system.key.test", "open");
    }

    @Override
    protected void onPause() {
        super.onPause();
        systemPropertiesProxy.set("persist.open.system.key.test", "close");

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("KeyDown", keyCode + "");
        switch (keyCode) {
            case KEYCODE_DPAD_F1:
                switchKeyBg(mKeyF1, false);
                break;
            case KEYCODE_DPAD_F2:
                switchKeyBg(mKeyF2, false);
                break;
            case KEYCODE_DPAD_SCAN1:
                switchKeyBg(mKeyScan1, false);
                break;
            case KEYCODE_DPAD_SCAN2:
                switchKeyBg(mKeyScan2, false);
                break;
            case KEYCODE_LEFT:
                switchKeyBg(mKeyLeft, false);
                break;
            case KEYCODE_RIGHT:
                switchKeyBg(mKeyRight, false);
                break;
            case KEYCODE_BACK:
                switchKeyBg(mKeyBack, false);
                break;
            case KEYCODE_ENTER:
                switchKeyBg(mKeyEnter, false);
                break;
            case KEYCODE_PLUS:
                switchKeyBg(mKeyJia, false);
                break;
            case KEYCODE_MINUS:
                switchKeyBg(mKeyJian, false);
                break;
            case KEYCODE_EDIT:
                switchKeyBg(mKeyEdit, false);
                break;
            case KEYCODE_MORE:
                switchKeyBg(mKeyMore, false);
                break;
            case KEYCODE_0:
                switchKeyBg(mKey0, false);
                break;
            case KEYCODE_1:
                switchKeyBg(mKey1, false);
                break;
            case KEYCODE_2:
                switchKeyBg(mKey2, false);
                break;
            case KEYCODE_3:
                switchKeyBg(mKey3, false);
                break;
            case KEYCODE_4:
                switchKeyBg(mKey4, false);
                break;
            case KEYCODE_5:
                switchKeyBg(mKey5, false);
                break;
            case KEYCODE_6:
                switchKeyBg(mKey6, false);
                break;
            case KEYCODE_7:
                switchKeyBg(mKey7, false);
                break;
            case KEYCODE_8:
                switchKeyBg(mKey8, false);
                break;
            case KEYCODE_9:
                switchKeyBg(mKey9, false);
                break;
        }
        return true;
    }

    int kCode;
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        Log.e("KeyDown", keyCode + "");
        kCode = keyCode;
        switch (keyCode) {
            case KEYCODE_DPAD_F1:
                keyPressF1 = true;
                switchKeyBg(mKeyF1, keyPressF1);
                break;
            case KEYCODE_DPAD_F2:
                keyPressF2 = true;
                switchKeyBg(mKeyF2, keyPressF2);
                break;
            case KEYCODE_DPAD_SCAN1:
                keyPressScan1 = true;
                switchKeyBg(mKeyScan1, keyPressScan1);
                break;
            case KEYCODE_DPAD_SCAN2:
                keyPressScan2 = true;
                switchKeyBg(mKeyScan2, keyPressScan2);
                break;
            case KEYCODE_LEFT:
                keyPressL = true;
                switchKeyBg(mKeyLeft, keyPressL);
                break;
            case KEYCODE_RIGHT:
                keyPressR = true;
                switchKeyBg(mKeyRight, keyPressR);
                break;
            case KEYCODE_BACK:
                if (isBack) {
                    finish();
                    isBack = false;
                    return false;
                }
                isBack = true;
                keyPressBack = true;
                switchKeyBg(mKeyBack, keyPressBack);
                break;
            case KEYCODE_ENTER:
                keyPressEnter = true;
                switchKeyBg(mKeyEnter, keyPressEnter);
                break;
            case KEYCODE_PLUS:
                keyPressJia = true;
                switchKeyBg(mKeyJia, keyPressJia);
                break;
            case KEYCODE_MINUS:
                keyPressJian = true;
                switchKeyBg(mKeyJian, keyPressJian);
                break;
            case KEYCODE_EDIT:
                keyPressEdit = true;
                switchKeyBg(mKeyEdit, keyPressEdit);
                break;
            case KEYCODE_MORE:
                keyPressMore = true;
                switchKeyBg(mKeyMore, keyPressMore);
                break;
            case KEYCODE_0:
                keyPressK0 = true;
                switchKeyBg(mKey0, keyPressK0);
                break;
            case KEYCODE_1:
                keyPressK1 = true;
                switchKeyBg(mKey1, keyPressK1);
                break;
            case KEYCODE_2:
                keyPressK2 = true;
                switchKeyBg(mKey2, keyPressK2);
                break;
            case KEYCODE_3:
                keyPressK3 = true;
                switchKeyBg(mKey3, keyPressK3);
                break;
            case KEYCODE_4:
                keyPressK4 = true;
                switchKeyBg(mKey4, keyPressK4);
                break;
            case KEYCODE_5:
                keyPressK5 = true;
                switchKeyBg(mKey5, keyPressK5);
                break;
            case KEYCODE_6:
                keyPressK6 = true;
                switchKeyBg(mKey6, keyPressK6);
                break;
            case KEYCODE_7:
                keyPressK7 = true;
                switchKeyBg(mKey7, keyPressK7);
                break;
            case KEYCODE_8:
                keyPressK8 = true;
                switchKeyBg(mKey8, keyPressK8);
                break;
            case KEYCODE_9:
                keyPressK9 = true;
                switchKeyBg(mKey9, keyPressK9);
                break;
        }

        if (keyPressR && keyPressL && keyPressScan1 && keyPressScan2 && keyPressMore && keyPressEnter
                && keyPressJian && keyPressEdit && keyPressJia && keyPressF1 && keyPressF2
                && keyPressBack
                && keyPressK0 && keyPressK1 && keyPressK2 && keyPressK3 && keyPressK4
                && keyPressK5 && keyPressK6 && keyPressK7 && keyPressK8 && keyPressK9
        ) {
        }

        return true;
    }


    private void switchKeyBg(TextView textView, boolean clik) {
        if (clik) {
            tvKey.setText(String.format("按键值:【%s】<-->【%d】", textView.getText(), kCode));
            textView.setBackgroundResource(R.drawable.key_press_bg_blue);
        } else {
            textView.setBackgroundResource(R.drawable.textview_border_err);
        }
    }


    class SystemPropertiesProxy {
        public String get(String key) {
            String result = "";
            try {
                Class<?> c = Class.forName("android.os.SystemProperties");

                Method get = c.getMethod("get", String.class);
                result = (String) get.invoke(c, key);

            } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
                     IllegalArgumentException | InvocationTargetException e) {
                e.printStackTrace();
            }
            return result;
        }

        public void set(String key, String val) {
            try {
                Class<?> c = Class.forName("android.os.SystemProperties");
                Method set = c.getMethod("set", String.class, String.class);
                set.invoke(c, key, val);
            } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
                     IllegalArgumentException | InvocationTargetException e) {
                e.printStackTrace();
            }

        }
    }


}